package logic;

import gui.GuiMain;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import memory.GenericEvents;
import memory.Memory;

class action extends TimerTask {
	private GenericEvents item;
	private Memory memory;
	
	//@author A0096677H
	// Calls refreshMark with a delay of 1 seconds to accommodate inaccuracies in timer
	// Updates changes in quick view
	public static void timerActivate(GenericEvents item, Memory memory){
		Mark.refreshMark(memory,1000);
		GuiMain.updateQuickView();
	}
	
	//@author A0096677H
	// Constructor
	public action(GenericEvents item, Memory memory) {
		this.item = item;
		this.memory = memory;
	}

	//@author A0096677H
	// Runs timerActivate
	@Override
	public void run() {
		timerActivate(item, memory);
	}
}

public class Time {
	public static Timer timer= new Timer();

	//@author A0096677H
	// Schedule a timer for all items in memory that has time elements involved
	public static void initTimer(Memory memory) {
		timer = new Timer();
		for (int i=0; i<memory.getSize(); i++) {
			if (memory.get(i).getType().equals("Deadline") || memory.get(i).getType().equals("Event") ){
				scheduleTimer(memory.get(i), timer, memory);
			}
		}
	}

	//@author A0096677H
	// Schedule timer according to start date and end date whenever available
	public static void scheduleTimer(GenericEvents item, Timer timer, Memory memory) {
		if(item.getStart()!=null && item.getStart().getTime().after(Calendar.getInstance().getTime())){
			timer.schedule(new action(item, memory), item.getStart().getTime());
		}
		if(item.getEnd()!=null && item.getEnd().getTime().after(Calendar.getInstance().getTime())){
			timer.schedule(new action(item, memory), item.getEnd().getTime());
		}
	}

}